package com.seirsoft.cookingtime.ui.dialog;

import org.eclipse.jface.viewers.ITableLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;

import com.seirsoft.cookingtime.controller.CtlDlgTimeSelect;
import com.seirsoft.cookingtime.model.TimeElement;
import com.seirsoft.cookingtime.ui.controls.sierTableViewer;
import com.seirsoft.cookingtime.view.IDlgTimeSelect;
import com.seirsoft.framework.util.GUIUtil;

public class DialogTimeSelect extends DialogBase 
implements IDlgTimeSelect
{
	
	private sierTableViewer m_tableviewer = null;
	private Button m_btnOK = null;
	
	private ToolItem m_itemAdd = null;
	private ToolItem m_itemDelete = null;
	private ToolItem m_itemSave = null;
	
	private CtlDlgTimeSelect getController()
	{
		return (CtlDlgTimeSelect) super.m_ctlBase;
	}
	
	public DialogTimeSelect(Shell parentShell) {
		super(parentShell);
	}
	
	@Override
	protected void createUI(Composite parent) 
	{
		GUIUtil.setGridLayout(parent, true, 1, 5, 5);
		
		this.createCoolbar(parent);

		this.createTable(parent);
		
		this.creatButtons(parent);
	}

	private void createCoolbar(Composite parent) {
		final ToolBar toolBar = new ToolBar(parent, SWT.WRAP);

		m_itemAdd = new ToolItem(toolBar, SWT.PUSH);
		m_itemAdd.setText("Add");

		m_itemDelete = new ToolItem(toolBar, SWT.PUSH);
		m_itemDelete.setText("Delete");

		m_itemSave = new ToolItem(toolBar, SWT.PUSH);
		m_itemSave.setText("Save");
	}
	
	public void setDeleteEnable(boolean enabled)
	{
		this.m_itemDelete.setEnabled(enabled);
	}

	private void createTable(Composite parent) {
		GridData layoutData = new GridData(GridData.FILL_HORIZONTAL);
		layoutData.grabExcessVerticalSpace = true;
		layoutData.minimumHeight = 100;
		layoutData.widthHint = 300;
	
		m_tableviewer = new sierTableViewer(parent,  SWT.FULL_SELECTION | SWT.BORDER | SWT.V_SCROLL | SWT.H_SCROLL);
		this.m_tableviewer.setLabelProvider(new TableLabelProvider());
		
		Table table = m_tableviewer.getTable();
		table.setLayoutData(layoutData);

	    table.setHeaderVisible(true);
	    table.setLinesVisible(true);
	    
	    Font font = new Font (parent.getDisplay(), "����", 15, SWT.BORDER); 
	    table.setFont(font);
	    
        table.addListener(SWT.MeasureItem, new Listener() {
            public void handleEvent(Event event)
            {
                event.height = event.gc.getFontMetrics().getHeight() + 4;
            }
        });
        
        getController().setTimeTable(m_tableviewer);
	}

	private void creatButtons(Composite parent)
	{
		//m_btnOK = this.createButton(parent, "&OK");
	}

	@Override
	public void fireSelectedEvent(SelectionEvent evt) {

		if(evt.getSource() == this.m_itemAdd)
		{
			this.getController().fireEventToolbarClickAdd();
		}
		else if(evt.getSource() == this.m_itemSave)
		{
			this.getController().fireEventToolbarClickSave();
		}
		else if(evt.getSource() == this.m_itemDelete)
		{
			this.getController().fireEventToolbarClickDelete();
		}
		else if(evt.getSource() == this.m_btnOK)
		{
			this.getController().fireEventButtonClickOK();
		}
		
	}


	@Override
	protected void addListener() {
		//m_btnOK.addSelectionListener(this);
		
		m_itemAdd.addSelectionListener(this);
		m_itemDelete.addSelectionListener(this);
		m_itemSave.addSelectionListener(this);

	}

	public class TableLabelProvider extends LabelProvider implements ITableLabelProvider {
        public String getColumnText(Object element, int columnIndex) {
            if (element instanceof TimeElement){
               	TimeElement objTimeElement = (TimeElement) element;
            	
            	return getController().fireEventGetColumnText(objTimeElement, columnIndex);
            }
            return null;
        }
        
        public Image getColumnImage(Object element, int columnIndex) {
            return null;
        }
    }
}
